package com.wust.supermarket.controller;

import com.wust.supermarket.domain.Response;
import com.wust.supermarket.domain.Staff;
import com.wust.supermarket.service.StaffService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
public class StaffController {
    @Autowired
    private StaffService staffService;
    @RequestMapping(value = "/staff/listStaff",method = RequestMethod.GET)
    public String ListStaff(Model model) {
        List<Staff> staffes=staffService.listAllStaffs();
        model.addAttribute("staffs", staffes);
        return "staff/listStaff";
    }

    @RequestMapping(value = "/staff/addStaff",method = RequestMethod.GET)
    public String AddStaff() {
        return "staff/addStaff";
    }

    @PostMapping("/staff/update")
    public Response updateUser(@RequestBody Staff staff){
        System.out.println(staff);
        if (staffService.updateStaff(staff)>0)
            return new Response<String>(1, "update staff success", null);
        return new Response<String>(1, "update staff failed", null);
    }

    @GetMapping("/staff/delete/{id}")
    @ResponseBody
    public Response deleteStaffByid(@PathVariable("id") int StaffId) {
        int i=staffService.deleteStaff(StaffId);
        if(i>0)
            return new Response<String>(1, "delete staff success", null);
        return new Response<String>(-1, " delete staff failed", null);

    }

    @GetMapping("/staff/getStaffByid/{id}")
    @ResponseBody
    public Staff getStaffByid(@PathVariable("id") int StaffId) {
        Staff staff=staffService.getStaffById(StaffId);
        return staff;
    }

    @PostMapping("/staff/add")
    public Response addStaff(@RequestBody Staff staff){
        System.out.println(staff);
        if (staffService.insertStaff(staff)>0)
            return new Response<String>(1, "add staff success", null);
        return new Response<String>(1, "add staff failed", null);
    }
}
